/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webClient;

import appClient.ConnectClient;
import com.google.gson.Gson;
import engine.Address;
import engine.City;
import engine.Country;
import engine.FullPersone;
import engine.GType;
import engine.Persone;
import engine.Phone;
import engine.Singleton;
import engine.Street;
import engine.TypeAdress;
import engine.TypePhone;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import protocol.Command;

/**
 *
 * @author User
 */
public class WebFunction {

    
  private ConnectClient connectClient;
  
  public static void setTable(List<Persone> list, ConnectClient connectClient) 
  {
      String newWindow = "ONCLICK=\"window.open('http://localhost:7777/formCode','','Toolbar=0,Location=0,Directories=0,Status=0,Menubar=0,Scrollbars=0,Resizable=0,Width=650,Height=550');\"";
      Singleton.table = "<table width=\"100%\" border=\"1\">";
      for (Persone p : list) {
      Singleton.table += "<tr>";
      Singleton.table += "<td>" + p.ID +"</td>";
      Singleton.table += "<td> <a href=\"?persone=" + p.ID + "\" " + newWindow + ">" + p.FName +"</a> </td>";
      Singleton.table += "<td>" + p.LNmae +"</td>";
      Singleton.table += "</tr>";
    }
      Singleton.table += "</table>";
    
    System.out.println(Singleton.table);
 
  }
  
  public static String setTels(String str)
  {
      String tel = "";
      int count = 1;
      for (Phone phon : Singleton.fp.Phones)
      {
          tel += "<tr>";
          tel += "<td><select name=\"telPype" + count + "\">";
          for (TypePhone p : Singleton.list) {
              if (phon.Type.Type.equals(p.Type))
              {
                tel += "<option selected>" + p.Type + "</option>";
              } else {
                tel += "<option>" + p.Type + "</option>";  
              }
              
          }
          tel += "</select></td>";
          
          tel += "<td><input type=\"text\" name=\"telNumber" + count + "\" value=\"" + phon.Number + "\" size=\"12\"/></td>";
          tel += "<td><input type=\"text\" name=\"telID" + count + "\" value=\"" + phon.ID + "\" size=\"2\"/></td>";
          tel += "<td><input type=\"checkbox\" name=\"telCheck" + count + "\" value=\"checkbox\" /></td>";
          tel += "</tr>";
          count++;
      }
      
      
      str = str.replace("$Phones", tel + "$Phones");
      return str;
  }
  
    public static String setAdr(String str) {
      String tel = "";
      int count = 1;
      for (Address adr : Singleton.fp.Addres)
      {
          tel += "<tr>";
          
          tel += "<td><select name=\"adrType" + count + "\">";
          for (TypeAdress p : Singleton.typeAdr) {
              if (adr.Type.Type.equals(p.Type))
              {
                tel += "<option selected>" + p.Type + "</option>";
              } else {
                tel += "<option>" + p.Type + "</option>";  
              }
          }
          tel += "</select></td>";
          
          tel += "<td><select name=\"country" + count + "\">";
          for (Country c : Singleton.country) {
              if (adr.country.name.equals(c.name))
              {
                tel += "<option selected>" + c.name + "</option>";
              } else {
                tel += "<option>" + c.name + "</option>";  
              }
          }
          tel += "</select></td>";
          
          List<City> city = Singleton.city;
          
          tel += "<td><input type=\"submit\" name=\"GetCity" + count + "\" value=\"GetCity\" />"
                  + "<select name=\"city" + count + "\"> $citylist" + count;
          tel += "<option selected>" + adr.city.name + "</option>";
          if (Singleton.city != null){
              for (City c : Singleton.city) {
                  tel += "<option>" + c.name + "</option>"; 
              }
          }
          tel += "</select></td>";
          
          tel += "<td><input type=\"submit\" name=\"GetStreet" + count + "\" value=\"GetStreet\" />"
                  + "<select name=\"street" + count + "\"> $streetlist" + count;
          tel += "<option selected>" + adr.street.name + "</option>";
          if (Singleton.street != null){
              for (Street c : Singleton.street) {
                  tel += "<option>" + c.name + "</option>"; 
              }
          }
          tel += "</select></td>";
          
          tel += "<td><input type=\"text\" name=\"flat" + count + "\" value=\"" + adr.Add + "\" size=\"12\"/></td>";
          tel += "<td><input type=\"text\" name=\"AdrId" + count + "\" value=\"" + adr.ID + "\" size=\"2\"/></td>";
          tel += "<td><input type=\"checkbox\" name=\"checkAdr" + count + "\" value=\"checkbox\" /></td>";
          tel += "</tr>";
          count++;
      }
      
      
      str = str.replace("$Adres", tel + "$Adres");
      return str;
    }
    
    public static void delPersone(FullPersone fp) {
      Command obj = new Command();
      Gson gson = new Gson();
      obj.Type = "Del";
      GType gt = new GType();
      gt.Type = "Person";
      obj.Data = gson.toJson(gt);
      obj.Add = gson.toJson(fp);;
      String res = gson.toJson(obj);
      Singleton.cli.refuse(res);
  }
  
}
